package com.ja.openchat.ui.chat.ext;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.ja.openchat.BuildConfig;
import com.ja.openchat.R;
import com.zy.openchat.core.model.message.ChatInfo;
import com.ja.openchat.ui.chat.annotation.ExtContextMenuItem;
import com.ja.openchat.ui.chat.ext.core.ConversationExt;
import com.zy.openchat.util.FileUtils;

import java.io.File;


public class FileExt extends ConversationExt {



    /**
     * @param containerView 扩展view的container
     * @param conversation
     */
    @ExtContextMenuItem
    public void pickFile(View containerView, ChatInfo conversation) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");//无类型限制
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, 100);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK&&data != null) {
            Uri uri = data.getData();
            // TODO Android10之后，可能不能直接通过uri拿到path，这时候，会进行文件拷贝
            // 当大文件上时，不应当进行拷贝，可以直接通过InputStream进行上传
            String path = FileUtils.getPath(activity, uri);
            if (TextUtils.isEmpty(path)) {
                ToastUtils.showShort(activity.getString(R.string.error_text));
                return;
            }
            File file = new File(path);
            if(file.length() > BuildConfig.SendFileMaxSize) {
                ToastUtils.showShort(String.format(activity.getString(R.string.file_max_size_hint),FileUtils.getReadableFileSize(BuildConfig.SendFileMaxSize)));
                return;
            }
            messageViewModel.sendFileMessage(conversation,file);
        }
    }

    @Override
    public boolean filter(ChatInfo conversation) {
        return conversation.isGroupSendChat();
    }

    @Override
    public int priority() {
        return 5;
    }

    @Override
    public int iconResId() {
        return R.drawable.icon_file;
//        return R.mipmap.menu_file;
    }

    @Override
    public String title(Context context) {
        return context.getString(R.string.file_text);
    }

    @Override
    public String contextMenuTitle(Context context, String tag) {
        return title(context);
    }
}
